package com.hboxs.asl.controller.app.member;

import com.hboxs.asl.JsonMessage;
import com.hboxs.asl.Page;
import com.hboxs.asl.Pageable;
import com.hboxs.asl.controller.app.BaseController;
import com.hboxs.asl.entity.Member;
import com.hboxs.asl.entity.Message;
import com.hboxs.asl.service.MemberService;
import com.hboxs.asl.service.MessageService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller(value = "appMMessageController")
@RequestMapping(value = "/app/m/message")
public class MessageController extends BaseController {

    @Resource(name = "messageServiceImpl")
    private MessageService messageService;
    @Resource(name = "memberServiceImpl")
    private MemberService memberService;

    /**
     * 获取我的消息列表
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public
    @ResponseBody
    JsonMessage list(Pageable pageable) {
        try {
            Member member = memberService.getCurrentByToken();
            if(member == null) {
                return JsonMessage.USER_NOT_LOGIN;
            }
            Page<Message> page = messageService.findPage(member, pageable);
            return JsonMessage.success(
                    "totalPage", page.getTotalPages(),
                    "pageNumber", page.getPageNumber(),
                    "pageSize", page.getPageSize(),
                    "total", page.getTotal(),
                    "content", JsonMessage.optimiseObj(page.getContent(), "id", "title", "createDateF", "content", "mosaicContent",
                            "receiver.name", "receiver.avator", "sender.name", "sender.avator"));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.error("没有收到消息");
    }

    /**
     * 获取消息详情
     * @param id
     * @return
     */
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public
    @ResponseBody
    JsonMessage detail(Long id) {
        try {
            Member member = memberService.getCurrentByToken();
            if(member == null) {
                return JsonMessage.USER_NOT_LOGIN;
            }
            com.hboxs.asl.entity.Message message = messageService.find(id);
            if(message == null) {
                return JsonMessage.error("找不到消息");
            }
            return JsonMessage.success(JsonMessage.optimiseObj(message, "createDate", "title", "content"));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }

    /**
     * 删除指定单条信息
     * @param messageId
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public
    @ResponseBody
    JsonMessage delete(Long messageId) {
        try {
            Member member = memberService.getCurrentByToken();
            if (member == null) {
                return JsonMessage.USER_NOT_LOGIN;
            }
            messageService.delete(messageId);
            return JsonMessage.success();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }

}
